<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="甲方识别码" prop="nbajbh">
        <el-input
          v-model="queryParams.nbajbh"
          placeholder="请输入甲方识别码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="案件名称" prop="ajmc">
        <el-input
          v-model="queryParams.ajmc"
          placeholder="请输入案件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务类型" prop="ywlx">
        <el-select v-model="queryParams.ywlx" placeholder="请选择业务类型" clearable @change="handleQuery">
          <el-option
            v-for="dict in dict.type.fs_post_ywlx"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="zt">
        <el-select v-model="queryParams.zt" placeholder="请选择状态" clearable @change="handleQuery">
          <el-option
            v-for="dict in dict.type.fs_task_zt"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fs:post:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList">
      <el-table-column label="甲方识别码" align="center" prop="nbajbh"/>
      <el-table-column label="案件名称" align="center" prop="ajmc"/>
      <el-table-column label="业务类型" align="center" prop="ywlx">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fs_post_ywlx" :value="scope.row.ywlx"/>
        </template>
      </el-table-column>
      <el-table-column label="收件信息" align="center" prop="sjr">
        <template slot-scope="scope">
          <span>{{ scope.row.sjr }}</span>
          &nbsp;&nbsp;
          <span>{{ scope.row.dh }}</span>
          <br>
          <span>{{ scope.row.dz }}</span>
        </template>
      </el-table-column>
      <el-table-column label="邮寄要求" align="center" prop="yjyq" width="200px" :show-overflow-tooltip="toolTip"/>
      <el-table-column label="状态" align="center" prop="zt">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fs_task_zt" :value="scope.row.zt"/>
        </template>
      </el-table-column>
      <el-table-column label="创建" align="center" prop="cjrMc">
        <template slot-scope="scope">
          <span>{{ scope.row.cjrMc }}</span>
          <br>
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成" align="center" prop="cjrMc">
        <template slot-scope="scope">
          <span>{{ scope.row.ssrMc }}</span>
          <br>
          <span>{{ parseTime(scope.row.wcsj, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding">
        <template slot-scope="scope">
          <el-button v-if="scope.row.zt==1"
                     size="mini"
                     type="text"
                     icon="el-icon-caret-right"
                     @click="handleStart(scope.row)"
                     v-hasPermi="['fs:post:edit']"
          >开始
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleZjcl(scope.row)"
            v-hasPermi="['fs:case:edit']"
          >证据
          </el-button>
          <el-button v-if="scope.row.zt==3"
                     size="mini"
                     type="text"
                     icon="el-icon-position"
                     @click="handleWl(scope.row)"
                     v-hasPermi="['fs:case:edit']"
          >物流
          </el-button>
          <el-button v-if="scope.row.zt==2"
                     size="mini"
                     type="text"
                     icon="el-icon-finished"
                     @click="handleUpdate(scope.row)"
                     v-hasPermi="['fs:post:edit']"
          >完成
          </el-button>
          <el-button v-if="scope.row.zt===1"
                     size="mini"
                     type="text"
                     icon="el-icon-delete"
                     @click="handleDelete(scope.row)"
                     v-hasPermi="['fs:post:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改邮寄工单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="快递公司" prop="com">
          <el-select v-model="form.com" placeholder="请选择快递公司">
            <el-option
              v-for="dict in dict.type.fs_kdgs_bm"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="快递单号" prop="kddh">
          <el-input v-model="form.kddh" placeholder="请输入快递单号"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 证据弹出框 -->
    <el-dialog :title="zjTitle" :visible.sync="zjOpen" width="800px" append-to-body>

      <el-table :data="zjList">
        <el-table-column label="证据名称" align="center" prop="typeName" width="180px"/>
        <el-table-column label="查看文件" align="center" prop="fileName">
          <template slot-scope="scope">
            <el-link :href="scope.row.oosUrl" target="_blank" class="buttonText" type="primary" :underline="false">
              {{ scope.row.fileName }}
            </el-link>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="zjcancel">关 闭</el-button>
      </div>
    </el-dialog>


    <!-- 快递信息 -->
    <el-dialog :title="yjxxTitle" :visible.sync="yjxxOpen" width="600px" append-to-body>
      <el-form ref="yjform" :model="yjform" label-width="120px">
        <el-row>
          <el-col v-if="yjform.com != null" :span="24">
            <el-form-item label="快递公司：">
              <dict-tag :options="dict.type.fs_kdgs_bm" :value="yjform.com"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="快递单号：">{{ yjform.kddh }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="业务类型：">
              <dict-tag :options="dict.type.fs_post_ywlx" :value="yjform.ywlx"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="业务状态：">
              <dict-tag :options="dict.type.fs_post_ywzt" :value="yjform.ywzt"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发件时间：">{{ yjform.fjsj }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="签收时间：">{{ yjform.qssj }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收件人：">{{ yjform.sjr }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话：">{{ yjform.dh }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="地址：">{{ yjform.dz }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注：">{{ yjform.remark }}</el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="yjform.kdzt != null">
          <el-col :span="24">
            <el-collapse v-model="kdxxCollapse">
              <el-collapse-item title="物流信息" name="1">
                <div>
                  <el-form-item label="物流状态：">
                    <dict-tag :options="dict.type.fs_post_status" :value="yjform.kdzt"/>
                  </el-form-item>
                </div>
                <div class="block">
                  <el-timeline :reverse="reverse">
                    <el-timeline-item
                      v-for="(activity, index) in wlxxDetail"
                      :key="index"
                      :timestamp="activity.time">
                      {{ activity.desc }}
                    </el-timeline-item>
                  </el-timeline>
                </div>
              </el-collapse-item>
            </el-collapse>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="yjxxcancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listPost, getPost, delPost, addPost, updatePost, start, wl} from "@/api/fs/post";
import {listFile,} from "@/api/fs/case";
import {parseTime} from "../../../utils/ruoyi";

export default {
  name: "Post",
  dicts: ['fs_post_ywlx', 'fs_task_zt', 'fs_kdgs_bm', 'fs_post_status', 'fs_post_ywzt'],
  data() {
    return {
      // 遮罩层
      loading: true,
      toolTip: true,
      // 选中数组
      ids: [],
      reverse: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 邮寄工单表格数据
      postList: [],
      zjList: [],
      wlxxDetail: [],
      kdxxCollapse: [],
      // 弹出层标题
      title: "",
      zjTitle: "",
      yjxxTitle: "",
      // 是否显示弹出层
      open: false,
      zjOpen: false,
      yjxxOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        caseId: null,
        nbajbh: null,
        ajmc: null,
        ywlx: null,
        sjr: null,
        dh: null,
        dz: null,
        yjyq: null,
        zt: null,
        cjr: null,
        ssr: null,
        fpsj: null,
        wcsj: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      yjform: {}
    };
  },
  created() {
    this.queryParams.zt = 1;
    this.getList();
  },
  methods: {
    parseTime,
    /** 查询邮寄工单列表 */
    getList() {
      this.loading = true;
      listPost(this.queryParams).then(response => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    yjxxcancel() {
      this.yjxxOpen = false;
    },
    zjcancel() {
      this.zjOpen = false;
      this.zjList = [];
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        caseId: null,
        nbajbh: null,
        ajmc: null,
        ywlx: null,
        sjr: null,
        dh: null,
        dz: null,
        yjyq: null,
        zt: null,
        cjr: null,
        ssr: null,
        fpsj: null,
        wcsj: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        kddh: null,
        com: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加邮寄工单";
    },
    /** 修改按钮操作 */
    handleStart(row) {
      this.reset();
      const id = row.id;
      start(id).then(response => {
        this.$modal.msgSuccess("已开始，请尽快完成");
        this.getList();
      });
    },
    handleWl(row) {
      const id = row.id;
      wl(id).then(response => {
        this.yjxxOpen = true;
        this.yjxxTitle = "快递信息";
        this.yjform = response.data;
        if (this.yjform != null && this.yjform.wlxx != null) {
          let wlxx = this.yjform.wlxx;
          let obj = eval(wlxx);
          this.wlxxDetail = obj;
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPost(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改邮寄工单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updatePost(this.form).then(response => {
            this.$modal.msgSuccess("已完成！");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除邮寄工单编号为"' + ids + '"的数据项？').then(function () {
        return delPost(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fs/post/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    },
    handleZjcl(row) {
      const id = row.caseId
      listFile(id).then(response => {
        console.log(this.response);
        this.zjList = response.data;
        this.zjOpen = true;
        this.zjTitle = "证据材料";
      });
    },
  }
};
</script>
